<template>
	<view class="content">
		<view class="login">
			<image class="logo" :src="wxConfig.accountInfo.icon" />
			<view class="title">{{wxConfig.accountInfo.nickname}}</view>
			<view class="text">首次使用需要授权才能使用哦</view>
			<button class="userlogin" @click="getUserInfoTap"
				:style="{backgroundColor: template.color.temp_color}">授权登录</button>
			<button class="userlogin navigateBack"
				:style="{backgroundColor: template.color.temp_color+'20', color: template.color.temp_color}"
				@click="navigateBack()">暂不登录</button>
		</view>
		<u-popup :show="editInfoShow" :round="10" :closeOnClickOverlay="false"
			mode="center" @close="editInfoShow=false">
			<view class="edit_info_box">
				<view class="head">
					<view class="title">完善信息</view>
					<view class="text">做个有身份的人～</view>
				</view>
				<view class="form_box">
					<form @submit="saveInfoFormTap">
						<view class="avatar">
							<image v-if="userInfoData.avatar_url" :src="userInfoData.avatar_url" mode="aspectFill"></image>
							<image v-else src="@/static/images/myCenter/avatar.png" mode="aspectFill"></image>
							<view class="edit" :style="{backgroundColor: template.color.temp_color}">
								<image src="https://app.daccf.com/cdn/01/101b22b617899d24677cfa851c73fd70.png"
									mode="aspectFill"></image>
							</view>
							<!-- 选择头像按钮 -->
							<button open-type="chooseAvatar" @chooseavatar="chooseavatar"></button>
							<!-- 选择名字 -->
							<input type="text" name="avatar_url" :value="userInfoData.avatar_url"
								style="display: none;" />
						</view>
						<view class="label">
							<view class="input">
								<input type="nickname" name="cname" :value="userInfoData.cname" placeholder="请输入昵称"
									placeholder-style="color:#bbb;" />
							</view>
						</view>
						<view class="button" :style="{backgroundColor: template.color.temp_color}">保存设置信息<button
								form-type="submit"></button></view>
						<view class="back" @click="navigateBack()">暂不完善</view>
					</form>
				</view>
		
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { getWeChartSmallProgramLoginInfo, dcH5WechatLogin, uploadImages, updateUserInfo } from '@/tools/request/api.js'
	import mixin from '@/tools/mixins/app_mixin.js';
	export default {
		name:"login",
		mixins: [mixin],
		props: {
			login_show: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				template: uni.getStorageSync('__ddminiTemplateConfig'),
				wxConfig: __wxConfig,
				wxCode: '',
				editInfoShow: false,
				userInfoData: {
					cname: '',
					avatar_url: ''
				}
			};
		},
		watch: {
			accessToken (newVal, oldVal) {
				if (newVal) {
					setTimeout(() => {
						this.editInfoShow = true;
					}, 500)
				}
			}
		},
		methods: {
			getUserInfoTap () {
				this.login();
			},
			
			chooseavatar(img) {
				uni.showLoading({
					title: '上传头像中',
					mask: true
				});
				let data = {
					file: img.detail.avatarUrl,
					type: 1
				}
				uploadImages(data).then(res => {
					if (res.code === 200) {
						uni.hideLoading();
						this.userInfoData.avatar_url = res.body;
					} else {
						uni.hideLoading();
						uni.showModal({
							content: res.msg,
							showCancel: false
						});
					}
				})
			},
			// 保存用户信息
			saveInfoFormTap (e) {
				let data = e.detail.value;
				let postdta = {
					headImg: this.userInfoData.avatar_url,
					userName: data.cname
				}
				if (!data.avatar_url) {
					return uni.showToast({
						title: '您还未上传头像',
						icon: 'none'
					});
				} else if (!data.cname) {
					return uni.showToast({
						title: '您还未填写昵称',
						icon: 'none'
					});
				}
				uni.showLoading({
					title: '更新中'
				})
				updateUserInfo(postdta).then(res => {
					if (res.code === 200) {
						uni.hideLoading();
						uni.showToast({
							title: '设置成功',
							mask: true
						});
						this.editInfoShow = false;
						this.getUserInfo();
						setTimeout(() => {
							uni.navigateBack();
						},500)
					} else {
						uni.showToast({
							title: '失败！' + res.msg,
							icon: 'none',
							mask: true
						});
					}
				})
			},
			navigateBack() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="less">
	page {
		background-color: #fff;
	}

	.login {
		height: 60vh;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;

		.logo {
			width: 120rpx;
			height: 120rpx;
			border-radius: 100%;
			border: 1px solid #efefef;
		}

		.title {
			color: #1f1f1f;
			font-size: 38rpx;
			padding-top: 20rpx;
			font-weight: normal;
		}

		.text {
			color: #666666;
			padding-top: 20rpx;
			font-size: 28rpx;
			padding-bottom: 60rpx;
			font-weight: normal;
		}

		.userlogin {
			background-color: #2e6cf6;
			width: 300rpx;
			height: 80rpx;
			font-size: 30rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 0;
			color: #fff;
			border-radius: 100rpx;
			margin-bottom: 20rpx;
			font-weight: normal;
		}

		.navigateBack {
			background-color: #e5edfe;
			color: #3d9ef9;
			margin-top: 10rpx;
		}

		button::after {
			border: 0;
		}
	}

	.edit_info_box {
		width: 600rpx;
		padding-bottom: 60rpx;

		.head {
			width: 80%;
			margin: 0 auto;
			padding: 50rpx 0;

			.title {
				color: #1f1f1f;
				font-size: 40rpx;
				font-weight: bold;
			}

			.text {
				color: #1f1f1f;
				font-size: 28rpx;
				padding-top: 6rpx;
			}
		}

		.form_box {
			width: 80%;
			margin: 0 auto;
			padding-top: 20rpx;

			.avatar {
				width: 160rpx;
				height: 160rpx;
				position: relative;
				margin: 0 auto;

				image {
					width: 100%;
					height: 100%;
					border-radius: 100%;
					border: 1px solid #f2f2f2;
				}

				button {
					position: absolute;
					width: 100%;
					height: 100%;
					top: 0;
					left: 0;
					opacity: 0;
				}

				.edit {
					background-color: #2e6cf6;
					width: 60rpx;
					height: 60rpx;
					border-radius: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
					position: absolute;
					bottom: 0;
					right: 0;

					image {
						width: 38rpx;
						height: 38rpx;
						border-radius: 0;
						border: none;
					}
				}
			}

			.label {
				width: 90%;
				margin: 0 auto;
				padding-top: 60rpx;

				.input {
					border: 1px solid #efefef;
					height: 80rpx;
					border-radius: 10rpx;
					display: flex;
					align-items: center;
					justify-content: center;

					input {
						width: 90%;
						height: 100%;
						color: #1f1f1f;
						font-size: 28rpx;
						text-align: center;
					}
				}
			}

			.button {
				background-color: #2e6cf6;
				width: 90%;
				margin: 0 auto;
				height: 80rpx;
				color: #fff;
				font-size: 30rpx;
				border-radius: 10rpx;
				margin-top: 50rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				position: relative;

				button {
					position: absolute;
					width: 100%;
					height: 100%;
					top: 0;
					left: 0;
					opacity: 0;
				}
			}

			.back {
				color: #999;
				width: 150rpx;
				height: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 0 auto;
				font-size: 26rpx;
				margin-top: 30rpx;
			}
		}
	}
	
</style>
